<?php

namespace App\Http\Controllers\Backend;

use App\Http\Requests\Role\CreateRequest;
use App\Http\Requests\Role\UpdateRequest;
use App\Http\Controllers\Controller;
use Spatie\Permission\Models\Permission;
use Spatie\Permission\Models\Role;

class RoleController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        $user = \Auth::user();
        if (!$user->hasPermissionTo('角色管理')) {
            return view('error.error');
        }
        $roles = Role::paginate(15);
        return view('role.list')->with(['roles' => $roles]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        $user = \Auth::user();
        if (!$user->hasPermissionTo('角色管理')) {
            return view('error.error');
        }
        $permissions = Permission::all();
        return view('role.create', [
            'permissions' => $permissions
        ]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param CreateRequest $request
     * @return \Illuminate\Http\Response
     * @throws \Throwable
     */
    public function store(CreateRequest $request)
    {
        $model = \DB::transaction(function () use ($request) {
            $attributes = $request->validated();
            $attributes['guard_name'] = 'admin';
            $permissions = $attributes['permissions'] ?? [];
            unset($attributes['permissions']);
            $model = Role::create($attributes);
            $permissions = Permission::find($permissions);
            !empty($permissions) && $model->syncPermissions($permissions);
            return $model;
        });
        return redirect()->route('role.index')->with('success', '添加成功');
    }

    /**
     * Display the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function edit(Role $role)
    {
        $user = \Auth::user();
        if (!$user->hasPermissionTo('角色管理')) {
            return view('error.error');
        }
        $ids = $role->permissions()->pluck('id')->toArray();
        $permissions = Permission::all();
        return view('role.create', [
            'role'        => $role,
            'permissions' => $permissions,
            'ids'         => $ids
        ]);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param UpdateRequest $request
     * @param Role $role
     * @return \Illuminate\Http\Response
     * @throws \Throwable
     */
    public function update(UpdateRequest $request, Role $role)
    {
        $model = \DB::transaction(function () use ($request, $role) {
            $attributes = $request->validated();
            $permissions = $attributes['permissions'] ?? [];
            unset($attributes['permissions']);
            $role->update($attributes);
            $permissions = Permission::find($permissions);
            !empty($permissions) && $role->syncPermissions($permissions);
            return $role;
        });
        return redirect()->route('role.index')->with('success', '编辑成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
